package com.itheima.d7_thread_communication;

public class Foodie implements Runnable{
    @Override
    public void run() {
//        while (true){
//            synchronized (Desk.class){
//                if(Desk.flag){
//                    System.out.println(Thread.currentThread().getName() + "吃包子");
//                    Desk.flag = false;
//                    try {
//                        Thread.sleep(50);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    // 通知厨师蒸包子
//                    Desk.class.notify();
//                    // 吃货自己等待
//                    try {
//                        Desk.class.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }else {
//                    System.out.println("吃货白跑一趟");
//                }
//            }
//        }

        while (true) {
            synchronized (Desk.class) {
                while (!Desk.flag) {
                    System.out.println("吃货白跑一趟");
                    try {
                        Desk.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(Thread.currentThread().getName() + "吃包子");
                Desk.flag = false;
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 通知所有厨师蒸包子
                Desk.class.notifyAll();
            }

        }

    }
}
